package miku.firstgame.view
{
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPWorld;

	import feathers.data.ListCollection;

	import flash.media.Video;

	import miku.firstgame.constrants.AppConstrants;
	import miku.firstgame.constrants.EventConstants;
	import miku.firstgame.model.Me;
	import miku.firstgame.model.Room;
	import miku.firstgame.model.User;
	import miku.firstgame.model.proxy.LobbyProxy;
	import miku.firstgame.model.proxy.ProxyHelper;
	import miku.firstgame.model.proxy.RoomProxy;
	import miku.firstgame.model.vo.TalkVo;
	import miku.firstgame.net.pack.EnterRoomRequestPack;
	import miku.firstgame.net.pack.TalkRequestPack;
	import miku.firstgame.world.GameRoomWorld;
	import miku.firstgame.world.RoomWorld;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	import starling.events.Event;

	public class RoomWorldMediator extends Mediator
	{
		public static const NAME:String = "roomWorldMediator";

		private var roomWorld:RoomWorld;

		public function RoomWorldMediator(viewComponent:Object = null)
		{
			super(NAME, viewComponent);
			this.roomWorld = viewComponent as RoomWorld;
		}

		override public function onRegister():void
		{
			super.onRegister();
			initUserSource();
			initRoomSource();
			roomWorld.messagePanel.addEventListener(EventConstants.LOBBY_MESSAGE_SEND, onMessageSend);
			roomWorld.roomList.addEventListener(Event.CHANGE, onRoomListClick);
		}

		override public function onRemove():void
		{
			super.onRemove();
			roomWorld.messagePanel.removeEventListener(EventConstants.LOBBY_MESSAGE_SEND, onMessageSend);
			roomWorld.roomList.removeEventListener(Event.CHANGE, onRoomListClick);
		}

		private function onRoomListClick(e:Event):void
		{
			var item:Room = roomWorld.roomList.selectedItem as Room;

			if (item)
			{
				if (item.userCount < item.maxUserCount)
				{
					var pack:EnterRoomRequestPack = new EnterRoomRequestPack();
					pack.roomId = item.roomId;
					ProxyHelper.send(pack);
				}
				else
				{
					trace("进入房间失败");
				}

			}

			//e.data is null
			//trace("onRoomListClick:" + e.data);
		}

		private function onMessageSend(e:Event):void
		{
//			var talkXml:XML = <t/>;
//			talkXml.@m = e.data.toString();
//			talkXml.@r = "-1";
//			talkXml.@uid = Me.Instace.id;
			var pack:TalkRequestPack = new TalkRequestPack();
			pack.message = e.data.toString();
			pack.roomId = -1;
			pack.userId = Me.Instace.id;
			ProxyHelper.send(pack);
		}

		private function initUserSource():void
		{
			var lobbyProxy:LobbyProxy = facade.retrieveProxy(LobbyProxy.NAME) as LobbyProxy;
			var users:Vector.<User> = lobbyProxy.getAllUser();
			roomWorld.userList.dataProvider = new ListCollection(users);
		}

		private function initRoomSource():void
		{
			var roomProxy:RoomProxy = facade.retrieveProxy(RoomProxy.NAME) as RoomProxy;
			var rooms:Vector.<Room> = roomProxy.getAllRoom();
			roomWorld.roomList.dataProvider = new ListCollection(rooms);
		}

		private function onRoomAddSuccess(room:Room):void
		{
			if (room.roomMaster.id == Me.Instace.id) //表示自己创建的房间
			{
				roomWorld.notifyRoomCreate(true);
				//TODO Switch to game room
				SP.world = new GameRoomWorld(room);
			}
			else //表示别人创建的房间
			{
				roomWorld.roomList.listCountChanged();
			}
		}

		private function onRoomRemoved():void
		{
			roomWorld.roomList.listCountChanged();
		}

		private function onRoomAddFail():void
		{
			roomWorld.notifyRoomCreate(false);
		}

		private function refreshUserList():void
		{
			roomWorld.userList.listCountChanged(); //需要考虑效率问题
		}

		private function receiveTalkMessage(talkVo:TalkVo):void
		{
			roomWorld.messagePanel.appendMessage(talkVo.user.name, talkVo.message);
			//roomWorld.messagePanel.newLine();
		}

		private function handleUserOnline(user:User):void
		{
			roomWorld.messagePanel.appendSysMessage(user.name + " " + "进入了房间");
			//roomWorld.messagePanel.newLine();
			refreshUserList();
		}

		private function handleUserOnffline(user:User):void
		{
			roomWorld.messagePanel.appendSysMessage(user.name + " " + "离开了房间");
			//roomWorld.messagePanel.newLine();
			refreshUserList();
		}

		private function onEnterRoomSuccess(room:Room):void
		{
			SP.world = new GameRoomWorld(room);
		}

		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case AppConstrants.ROOM_ADD_SUCCESS:
					onRoomAddSuccess(note.getBody() as Room);
					break;
				case AppConstrants.ROOM_ADD_FAIL:
					onRoomAddFail();
					break;
				case AppConstrants.USER_ONLINE:
					handleUserOnline(note.getBody() as User);
					break;
				case AppConstrants.USER_OFFLINE:
					handleUserOnffline(note.getBody() as User);
					break;
				case AppConstrants.RECEIVE_LOBBY_MESSAGE:
					receiveTalkMessage(note.getBody() as TalkVo);
					break;
				case AppConstrants.ROOM_REMOVED:
					onRoomRemoved();
					break;
				case AppConstrants.ENTER_ROOM_SUCCESS:
					onEnterRoomSuccess(note.getBody() as Room);
					break;
				case AppConstrants.ENTER_ROOM_FAIL:

					break;
				case AppConstrants.ENTER_ROOM_MAX_USER_FAIL:

					break;

			}
		}

		override public function listNotificationInterests():Array
		{
			return [
				AppConstrants.ROOM_REMOVED,
				AppConstrants.ROOM_ADD_SUCCESS,
				AppConstrants.ROOM_ADD_FAIL,
				AppConstrants.USER_ONLINE,
				AppConstrants.USER_OFFLINE,
				AppConstrants.RECEIVE_LOBBY_MESSAGE,
				AppConstrants.ENTER_ROOM_SUCCESS,
				AppConstrants.ENTER_ROOM_FAIL,
				AppConstrants.ENTER_ROOM_MAX_USER_FAIL
				];
		}
	}
}
